import * as types from '../mutation-types'
import { fetch } from 'utils/api.js';
import querystring from 'querystring';

const state = {
  response: {
    errorCode: "",
    errorMsg: "",
  },
  errorInputName: "",
  formDataInfo: {},
  formDataPrice: {},
  specialColumnId:"",
  coursePacketId:"",
  editingInfoData:{},
  editingPriceData:{},
  status:""
}

const mutations = {
  //更新表单数据
  [types.UPDATE_FORM_DATA_INFO] (state,formData) {
    //state.formData[name] = value;
    Object.assign(state.formDataInfo, formData);
  },
  [types.UPDATE_FORM_DATA_PRICE] (state,formData) {
    //state.formData[name] = value;
    Object.assign(state.formDataPrice, formData);
  },
  //提交失败
  [types.SUBMIT_ERROR] (state,{message,code}) {
    state.response.errorMsg = message;
    state.response.errorCode = code;
  },

  //提交失败
  [types.SUBMIT_ERROR_INPUT] (state,{name}) {
    state.errorInputName = name;
  },

  //专栏信息提交成功
  [types.SUBMIT_INFO_SUCCESS] (state,{specialColumnId}) {
    state.specialColumnId = specialColumnId;
  },

  //课程价格信息提交成功
  [types.SUBMIT_PRICE_SUCCESS] (state,{coursePacketId}) {
    state.coursePacketId = coursePacketId;
  },

  // 设置待修改内容(专栏)
  [types.SET_INFO_EDITING_DATA](state, editingData) {
    Object.assign(state.editingInfoData, editingData);
  },
  // 设置待修改内容(课程包)
  [types.SET_PRICE_EDITING_DATA](state, editingData) {
    Object.assign(state.editingPriceData, editingData);
  },
  // 创建新专栏
  [types.CREATE_INFO](state) {
    state.editingInfoData = {};
    state.editingPriceData = {};
  },
  [types.SET_COLUMN_STATUS](state,status) {
    state.status = status;
  }
}

const actions = {
  createInfo({commit}){
    return new Promise((resolve, reject) => {
        commit(types.CREATE_INFO);
        resolve();
    });
  },
  setColumnStatus({commit, state, dispatch},status){
    commit(types.SET_COLUMN_STATUS,status);
  },
  editInfo({commit, state, dispatch},specialColumnId){
    return new Promise((resolve, reject) => {
      fetch('specialColumnInfo', { body: { id: specialColumnId } }).then((res) => {
        if (res.code === 200) {
          commit(types.SET_INFO_EDITING_DATA, res.data);
          commit(types.SUBMIT_INFO_SUCCESS, {specialColumnId});
          resolve(res.data);
        } else {
          throw new Error('Response Error');
        }
      });
    });
  },
  editPrice({commit, state, dispatch},coursePacketId){
    return new Promise((resolve, reject) => {
      fetch('coursePacketInfo', { body: { id: coursePacketId } }).then((res) => {
        if (res.code === 200) {
          commit(types.SET_PRICE_EDITING_DATA, res.data);
          resolve(res.data);
        } else {
          throw new Error('Response Error');
        }
      });
    });
  },
  submitForm({commit, state, dispatch}){
    return new Promise((resolve, reject) => {
      let storeFormData = state.formDataInfo;
      fetch("saveSpecialColumn", { body:storeFormData}).then((data) => {
        // success callback
        if (data.code == 200) {
          dispatch("submitInfoSuccess", data.data); //专栏 id
          resolve(data.data);
        } else {
          dispatch("submitError", {'message': data.message, 'code': data.code});
          reject();
        }
      }, (data) => {
        // error callback
        dispatch("submitError", {'message': data.message, 'code': data.code});
        reject();
      });
    });
  },
  submitPrice({commit, state, dispatch}){
    let storeFormData = state.formDataPrice;
    fetch("saveCoursePacket", { body:storeFormData}).then((data) => {
      // success callback
      if (data.code == 200) {
        dispatch("submitPriceSuccess", data.data);//课程包 id
      } else {
        dispatch("submitError", {'message': data.message, 'code': data.code});
      }
    }, (data) => {
      // error callback
      dispatch("submitError", {'message': data.message, 'code': data.code});
    });
  },
  updateFormDataInfo ({commit}, formData) {
    commit(types.UPDATE_FORM_DATA_INFO, formData);
  },
  updateFormDataPrice ({commit}, formData) {
    commit(types.UPDATE_FORM_DATA_PRICE, formData);
  },
  submitError ({commit}, {message, code}) {
    commit(types.SUBMIT_ERROR, {message, code});
    alert(message)
  },
  submitInfoSuccess ({commit}, specialColumnId) {
    commit(types.SUBMIT_INFO_SUCCESS, {specialColumnId});
  },
  submitPriceSuccess({commit}, coursePacketId) {
    commit(types.SUBMIT_PRICE_SUCCESS, {coursePacketId});
    alert("提交成功");
    location.href = MAIN_HOST+"/index.html";
  }
}

export default {
  state,
  mutations,
  actions
}